package com.hzit.controller;

import com.hzit.entity.Content;
import com.hzit.service.ContentService;
import com.hzit.service.impl.FileService;
import com.cwy.hzit.utils.PageResult;
import com.hzit.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.cwy.hzit.utils.R;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {

	@Autowired
	private ContentService contentService;

	@Autowired
	private FileService fileService;


	@GetMapping("/list")
	public R getStudent() {
		return R.ok().data("list", contentService.list());
	}

	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
					@PathVariable("pageSize") Integer pageSize,
					@RequestBody(required = false) ContentVO vo) {
		PageResult<Content> pr = contentService.search(page, pageSize, vo);
		return R.ok().data("pr", pr);
	}

	@PostMapping("/add")
	public R add(@RequestBody Content content) {
		boolean flag = contentService.add(content);
		return flag ? R.ok().message("添加广告成功!") : R.error().message("添加广告失败!");
	}

	@PostMapping("/upload")
	public ResponseEntity<?> uploadImage(@RequestParam("file") MultipartFile file) {

		String pic = fileService.uploadFile(file);
		return ResponseEntity.ok(pic);
	}


	@PutMapping("/update")
	public R update(@RequestBody Content content) {
		boolean flag = contentService.updateById(content);
		return flag ? R.ok().message("修改广告成功!") : R.error().message("修改广告失败!");
	}


	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids) {
		List<String> contentIds = Arrays.asList(ids.split(","));
		return contentService.removeByIds(contentIds) ? R.ok().message("删除广告成功!") : R.error().message("删除广告失败!");
	}
}